/**
 * 职级套件
 */
import React, { useEffect, useState, useMemo } from 'react';
import { Form, Spin, message } from 'antd';

import { withRouter } from 'umi';
import styles from '@/pages/WorkFlow/components/CustomComponents/Recruitment/index.less';
import AutoRenderComponent from '@/pages/WorkFlow/components/FormControls/components/AutoRenderComponent/AutoRenderComponent';
import { getPostApplyData } from '@/pages/WorkFlow/services';
import BusinessForm from './BusinessForm';

import pageStyles from './index.less';

const formLayout = {
    wrapperCol: { span: 18 },
    labelCol: { span: 6 }
};

const OPERATION_TYPE = {
    0: '新增',
    1: '修改',
    2: '删除'
};

const PostRank = ({ form, location, wrapperLayout, dataSource, formValueMap }) => {
    const { children: list = [], tag } = dataSource;

    const [approveData, setApproveData] = useState(null); // 审批数据
    const [approveDataLoading, setApproveDataLoading] = useState(false);

    const { getFieldDecorator } = form;

    const hiDataId = location?.query?.hiDataId || formValueMap?.hiDataId;

    const isPostRankType = useMemo(() => {
        if (tag === 'OrgRankTypeApply') {
            return true;
        }
        return false;
    }, [tag]);

    // 拉取审批数据
    useEffect(() => {
        if (hiDataId) {
            const fetchApproveData = async () => {
                setApproveDataLoading(true);
                const { msg, code, data } = await getPostApplyData(hiDataId);
                if (code === 10000) {
                    setApproveData(data);
                } else {
                    message.error(msg);
                }
                setApproveDataLoading(false);
            };
            fetchApproveData();
        }
    }, [hiDataId]);

    // 如果是删除的话拿oldBusinessData数据源
    const initialApproveData = useMemo(() => {
        const { operationType, oldBusinessData } = approveData || {};
        if (operationType === 2 && oldBusinessData) {
            return oldBusinessData;
        }
        return approveData;
    }, [approveData]);

    const pageTitle = useMemo(() => {
        const { operationType } = approveData || {};
        if (operationType !== undefined && (tag === 'OrgRankTypeApply' || tag === 'OrgRankApply')) {
            const pageName = tag === 'OrgRankTypeApply' ? '职级序列' : '职级';
            switch (operationType) {
                case 0:
                    return `新增${pageName}`;
                case 1:
                    return `编辑${pageName}`;
                case 2:
                    return `删除${pageName}`;
                default:
                    return '';
            }
        }
        return '';
    }, [approveData, tag]);

    return (
        <div style={{ position: 'relative' }}>
            {pageTitle && (
                <header className={pageStyles.pageTitle}>
                    <p>{pageTitle}</p>
                </header>
            )}
            <div className={styles.formIntro}>
                <span>{tag === 'OrgRankTypeApply' ? '职级序列' : '职级'}申请流程</span>
                <div className={styles.info}>
                    以下是{tag === 'OrgRankTypeApply' ? '职级序列' : '职级'}申请详情，请审阅
                </div>
            </div>
            <Spin spinning={approveDataLoading}>
                <Form layout="horizontal" className={styles.postEdit} {...wrapperLayout}>
                    {list.map(item => {
                        const { label, formId, rules, extra } = item;
                        let { initialValue } = item;

                        // 审批信息的id跟着流程走
                        if (formId === 'hiDataId') {
                            initialValue = hiDataId;
                        }
                        if (formId === 'rankCode') {
                            initialValue = initialApproveData?.institutionType;
                        }
                        if (formId === 'rankCodeName') {
                            initialValue = '人事制度';
                        }
                        if (formId === 'institutionNo') {
                            initialValue = initialApproveData?.institutionNo;
                        }
                        if (formId === 'institutionIntroduce') {
                            initialValue = initialApproveData?.institutionIntroduce;
                        }
                        if (formId === 'operationType') {
                            initialValue = OPERATION_TYPE[approveData?.operationType];
                        }
                        return (
                            <Form.Item
                                label={label}
                                key={formId}
                                extra={extra}
                                style={{ display: item?.inRenderDom === false ? 'none' : 'block' }}
                            >
                                {getFieldDecorator(formId, {
                                    initialValue,
                                    rules
                                })(<AutoRenderComponent form={form} config={item} />)}
                            </Form.Item>
                        );
                    })}

                    <BusinessForm
                        approveData={approveData}
                        isPostRankType={isPostRankType}
                        formLayout={formLayout}
                        wrapperLayout={wrapperLayout}
                        tag={tag}
                    />
                </Form>
            </Spin>
        </div>
    );
};

export default withRouter(PostRank);
